package
{
	import as3isolib.core.ClassFactory;
	import as3isolib.core.IFactory;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.renderers.DefaultShadowRenderer;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import mx.core.IFactory;
	
	public class rts1 extends Sprite
	{
		public static const ACTION_BUY_GAS_GATHERER:int = 0;
		public static const ACTION_BUY_ATTACKER:int = 1;
		
		public static const RESOURCE_TYPE_GAS:int = 0;
		public static const RESOURCE_TYPE_MINERAL:int = 1;
		
		public static const GAME_WIDTH:int = 760;
		public static const GAME_HEIGHT:int = 600;
		public static const GRID_WIDTH:int = 10;
		public static const GRID_HEIGHT:int = 10;
		public static const RATIO:Point = new Point(GAME_WIDTH / GRID_WIDTH / 2, GAME_HEIGHT / GRID_HEIGHT / 2);
		
		private var m_terminal:VTTerm = new VTTerm(onTerminalCallback);
		private var m_lastTick:int;
		private var m_gameObjects:Vector.<IGameObject> = new Vector.<IGameObject>;
		
		private var m_goTimer:Timer = new Timer(10);
		
		
		private var m_myHomeBase:HomeBase;
		private var m_enemyHomeBase:HomeBase;
		
		public function rts1()
		{
			viewIso();
		}
		
		private function view2d():void{
			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
			m_goTimer.addEventListener(TimerEvent.TIMER, onGOTimerUpdate);
			//m_goTimer.start();
			
			m_lastTick = getTimer();
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			
			var gameArea:Sprite = new Sprite();
			gameArea.graphics.beginFill(0xaaaaaa);
			gameArea.graphics.drawRect(0,0,GAME_WIDTH,GAME_HEIGHT);
			gameArea.graphics.endFill();
			addChild(gameArea);
			gameArea.addChild(VTUtil.getGrid(GAME_WIDTH,GAME_HEIGHT,10,10,0x00ff00,1));
			
			var hb:HomeBase = m_myHomeBase = new HomeBase;
			hb.movementComponent.location = new Point(2,8);
			hb.movementComponent.destination = new Point(2,8);
			hb.displayContainer = this;
			hb.gasGathered = 60;
			gameArea.addChild(hb.display);
			m_gameObjects.push(hb);
			
			var gasNode:ResourceNode = new ResourceNode;
			gasNode.movementComponent.location = new Point(1,9);
			gasNode.update(0);
			gasNode.type = RESOURCE_TYPE_GAS;
			gameArea.addChild(gasNode.display);
			hb.gasNode = gasNode;
			
			var ehb:HomeBase = m_enemyHomeBase = new HomeBase;
			ehb.movementComponent.location = new Point(8,2);
			ehb.movementComponent.destination = new Point(8,2);
			ehb.displayContainer = this;
			gameArea.addChild(ehb.display);
			m_gameObjects.push(ehb);
			
			var enemyGasNode:ResourceNode = new ResourceNode;
			enemyGasNode.movementComponent.location = new Point(9,1);
			enemyGasNode.update(0);
			enemyGasNode.type = RESOURCE_TYPE_GAS;
			gameArea.addChild(enemyGasNode.display);			
			
			ehb.gasNode = enemyGasNode;
			
			hb.enemyHomeBase = ehb;
			ehb.enemyHomeBase = hb;
			
			addChild(m_terminal);
			
			var ai:AIController = new AIController();
			ai.apm = 15;
			ai.homeBase = ehb;
			ai.start();
			
			var hud:HUD = new HUD(hb);
			addChild(hud.display);
			hud.startUpdates();
			
		}
		
		private var m_isoScene:IsoScene = new IsoScene();
		private function viewIso():void{
			var grid:IsoGrid = new IsoGrid();
			grid.cellSize = RATIO.x;
			grid.setGridSize(GRID_WIDTH,GRID_HEIGHT);
			
			m_isoScene.addChild(grid);
			
			
			var view:IsoView = new IsoView();
			view.setSize(760, 600);
			view.addScene(m_isoScene);
			view.panTo(0,150);
			addChild(view);			

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;			
			m_lastTick = getTimer();
			m_goTimer.addEventListener(TimerEvent.TIMER, onGOTimerUpdate);
			m_goTimer.start();
			
			var hb:HomeBase = m_myHomeBase = new HomeBase;
			hb.movementComponent.location = new Point(0,0);
			hb.movementComponent.destination = new Point(5,8);
			hb.displayContainer = this;
			hb.gasGathered = 60;
			m_isoScene.addChild(hb.isoDisplay);
			m_gameObjects.push(hb);
//			
//			var gasNode:ResourceNode = new ResourceNode;
//			gasNode.movementComponent.location = new Point(1,9);
//			gasNode.update(0);
//			gasNode.type = RESOURCE_TYPE_GAS;
//			gameArea.addChild(gasNode.display);
//			hb.gasNode = gasNode;
//			
//			var ehb:HomeBase = m_enemyHomeBase = new HomeBase;
//			ehb.movementComponent.location = new Point(8,2);
//			ehb.movementComponent.destination = new Point(8,2);
//			ehb.displayContainer = this;
//			gameArea.addChild(ehb.getDisplay());
//			m_gameObjects.push(ehb);
//			
//			var enemyGasNode:ResourceNode = new ResourceNode;
//			enemyGasNode.movementComponent.location = new Point(9,1);
//			enemyGasNode.update(0);
//			enemyGasNode.type = RESOURCE_TYPE_GAS;
//			gameArea.addChild(enemyGasNode.display);			
//			
//			ehb.gasNode = enemyGasNode;
//			
//			hb.enemyHomeBase = ehb;
//			ehb.enemyHomeBase = hb;
			

//			var ai:AIController = new AIController();
//			ai.apm = 15;
//			ai.homeBase = ehb;
//			ai.start();

			
			addChild(m_terminal);
			var hud:HUD = new HUD(hb);
			addChild(hud.display);
			hud.startUpdates();
		}
		
		private function update():void{
			var currentTick:int = getTimer();
			var delta:int = currentTick - m_lastTick;
			m_lastTick = currentTick;
			for(var i:int=0, l:int=m_gameObjects.length; i<l; i++){
				m_gameObjects[i].update(delta);
			}
			
			m_isoScene.render();
		}
		
		private function onGOTimerUpdate(te:TimerEvent):void{
			update();
		}
		
		private function onEnterFrame(e:Event):void{
			update();
		}
		
		private function onTerminalCallback(command:String):void{
			switch(command){
				case "ag": // add gas
					m_myHomeBase.createGasGatherer();
					break;
				case "ba": // buy attacker
					m_myHomeBase.createAttacker();
					break;
				case "status":
					var status:String = 'gas: ' + m_myHomeBase.gasGathered + ', mineral: ' + m_myHomeBase.mineralGathered;
					m_terminal.write(status);
					break;
			}
		}
	}
}